<?php

	namespace Zhutong;
	
//	use Conn\HTTP\Client;

	class Zhutong
	{
		public static $_error = '';

		//行业普通
		const USERNAME = 'hzjdhy';
		const PASSWORD = '5eAcgq';
		//营销批量
		const MARKET_USERNAME = 'hzjdyx';
		const MARKET_PASSWORD = 'EP6X8s';
		//语音
		const VOICE_USERNAME = 'hzjdyzm';
		const VOICE_PASSWORD = 'VhYS7V';

		# 单条接口地址
		protected $_single_url = 'http://www.api.zthysms.com/sendSms.do';
		# 语音接口地址
		protected $_voice_url = 'http://www.yzmsms.cn/sendSmsYY.do';
		# 批量接口地址
		protected $_batch_url = 'http://www.api.zthysms.com/sendSmsBatch.do';
		# 批量定时接口地址（暂不支持）
		protected $_batch_time_url = 'http://www.api.zthysms.com/sendSmsBatchTiming.do';
		# 批量个性化接口地址
		protected $_batch_identity_url = 'http://www.api.zthysms.com/sendSmsBatchIdentity.do';

		protected $_tkey = 0;

		private static $instance = NULL;

		public static function getInstance()
		{
			if (self::$instance == NULL) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		public function getCommonFields($password)
		{
			$this->_tkey = date('YmdHis');

			return [
				'tkey' => $this->_tkey,
				'password' => md5(md5($password) . $this->_tkey),
			];
		}
 
		/**
		 * 发送单条消息
		 */
		public function sendSingleSms($phone, $content)
		{
			$param = array_merge([
				'username' => self::USERNAME,
				'mobile' => $phone,
				'content' => $content,
			],  $this->getCommonFields(self::PASSWORD));

//			\log::error("== send single sms request url :". $this->_single_url . "\n" . var_export($param, true));
			$sendRes = $this->sendRequest($this->_single_url, $param);
//			\log::error("== send single sms res :" . var_export($sendRes, true));
			$sendArr = explode(',', $sendRes);
			return $sendArr;
		}

		/**
		 * 发送语音
		 */
		public function sendVoiceSMs($phone, $content)
		{
			$param = array_merge([
				'username' => self::VOICE_USERNAME,
				'mobile' => $phone,
				'content' => $content,
			],  $this->getCommonFields(self::VOICE_PASSWORD));

//			\log::error("== send voice sms request url :". $this->_voice_url . "\n" . var_export($param, true) . "\n use username:" . self::VOICE_USERNAME . '\n use password:' . self::VOICE_PASSWORD);
			$sendRes = $this->sendRequest($this->_voice_url, $param);
//			\log::error("== send voice sms res :" . var_export($sendRes, true));
			$sendArr = explode(',', $sendRes);
			return $sendArr;
		}

		/**
		 * 批量发送消息
		 */
		public function sendBatchSms($phones, $content)
		{
			$param = array_merge([
				'username' => self::MARKET_USERNAME,
				'mobile' => $phones,
				'content' => $content,
			],  $this->getCommonFields(self::MARKET_PASSWORD));

//			\log::error("== send batch sms request url :". $this->_batch_url . "\n" . var_export($param, true));
			$sendRes = $this->sendRequest($this->_batch_url, $param);
//			\log::error("== send batch sms res :" . var_export($sendRes, true));
			$sendArr = explode(',', $sendRes);
			return $sendArr;
		}

		public function sendRequest($url, $param)
		{
			$requestRes = $this->sendPost($url, $param, [], true);
//			\log::info("== Zhu Tong Request res: " . var_export($requestRes, true));
			return $requestRes;
		}
	 
		public static function getError()
		{
			return self::$_error;
		}

		public function sendPost($url, $param, $header = [], $isPost = true)
		{
			$curl = curl_init(); // 启动一个CURL会话      
			curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址                  
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查      
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在      
			if (isset($_SERVER['HTTP_USER_AGENT'])) {
				curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
			}
			curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
			curl_setopt($curl, CURLOPT_POSTFIELDS,  http_build_query($param)); // Post提交的数据包
			curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环      
			curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容      
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
			$result = curl_exec($curl); // 执行操作      
			if (curl_errno($curl)) {      
				echo 'Error POST'.curl_error($curl);      
			}      
			curl_close($curl); // 关键CURL会话      
			return $result; // 返回数据    
		}
		
	}

?>